package com.qiaoda.service.impl;

import com.qiaoda.service.JestService;
import com.qiaoda.utils.IndexNameAndTypeConstant;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.*;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.indices.mapping.PutMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JestServiceImpl implements JestService {

    @Autowired
    private JestClient jestClient;

//    /**
//     * 获取JestClient对象，配置文件中已经指定
//     * @return
//     */
//    public JestClient getJestClient() {
//
//        JestClientFactory factory = new JestClientFactory();
//        factory.setHttpClientConfig(new HttpClientConfig
//                               .Builder("http://localhost:9200")
//                               .gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'hh:mm:ss").create())
//                               .connTimeout(1500)
//                               .readTimeout(3000)
//                               .multiThreaded(true)
//                               .build());
//        return factory.getObject();
//    }

    /**  
     * 创建索引  
     * @param indexName
     * @return  
     * @throws Exception
     */
    @Override
    public boolean createIndex(String indexName) throws Exception {
        JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());
        return jr.isSucceeded();  
    }  

    /**  
     * Put映射  
     * @param source
     * @return  
     * @throws Exception  
     */
    @Override
    public boolean createIndexMapping(String source) throws Exception {
  
        PutMapping putMapping = new PutMapping.Builder(IndexNameAndTypeConstant.INDEXNAME_MYINDEX, IndexNameAndTypeConstant.TYPENAME_USER, source).build();
        JestResult jr = jestClient.execute(putMapping);  
        return jr.isSucceeded();  
    }

    /**  
     * Get映射  
     * @return
     * @throws Exception  
     */
    @Override
    public String getIndexMapping() throws Exception {

        GetMapping getMapping = new GetMapping.Builder().addIndex(IndexNameAndTypeConstant.INDEXNAME_MYINDEX).addType(IndexNameAndTypeConstant.TYPENAME_USER).build();
        JestResult jr = jestClient.execute(getMapping);  
        return jr.getJsonString();  
    }

    /**  
     * 索引文档  
     * @param objs
     * @return  
     * @throws Exception  
     */
    @Override
    public boolean index(List<Object> objs) throws Exception {
          
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex(IndexNameAndTypeConstant.INDEXNAME_MYINDEX).defaultType(IndexNameAndTypeConstant.TYPENAME_USER);
        for (Object obj : objs) {  
          Index index = new Index.Builder(obj).build();
          bulk.addAction(index);  
        }  
        BulkResult br = jestClient.execute(bulk.build());
        return br.isSucceeded();  
    }

    /**  
     * 搜索文档  
     * @param query  理解成查询json字符串
     * @return  
     * @throws Exception  
     */
    @Override
    public SearchResult search(String query) throws Exception {
          
        Search search = new Search.Builder(query)
            .addIndex(IndexNameAndTypeConstant.INDEXNAME_MYINDEX)
            .addType(IndexNameAndTypeConstant.TYPENAME_USER)
            .build();  
        return jestClient.execute(search);  
    }

    /**  
     * Count文档  
     * @param query
     * @return  
     * @throws Exception  
     */
    @Override
    public Double count(String query) throws Exception {
  
        Count count = new Count.Builder()  
            .addIndex(IndexNameAndTypeConstant.INDEXNAME_MYINDEX)
            .addType(IndexNameAndTypeConstant.TYPENAME_USER)
            .query(query)  
            .build();  
        CountResult results = jestClient.execute(count);   
        return results.getCount();  
    }  

    /**  
     * Get文档  
     * @param id
     * @return  
     * @throws Exception  
     */
    @Override
    public JestResult get(String id) throws Exception {
            
        Get get = new Get.Builder(IndexNameAndTypeConstant.TYPENAME_USER, id).type(IndexNameAndTypeConstant.TYPENAME_USER).build();
        return jestClient.execute(get);  
    }  

    /**  
     * Delete索引  
     * @param indexName
     * @return  
     * @throws Exception  
     */
    @Override
    public boolean deleteIndex(String indexName) throws Exception {
        JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());
        return jr.isSucceeded();  
    }  
      
    /**  
     * Delete文档  
     * @param id
     * @return  
     * @throws Exception  
     */
    @Override
    public boolean delete(String id) throws Exception {
        DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(IndexNameAndTypeConstant.INDEXNAME_MYINDEX).type(IndexNameAndTypeConstant.TYPENAME_USER).build());
        return dr.isSucceeded();  
    }  
      
    /**
     * 关闭JestClient客户端
     * @throws Exception
     */
    @Override
    public void closeJestClient() throws Exception {
        if (jestClient != null) {
          jestClient.shutdownClient();
        }
    }

}